package com.water.devtool.ui;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.google.common.io.Files;
import com.water.devtool.constant.DbType;
import com.water.devtool.define.DbConfigBean;

public class DbConfigDialog extends Dialog {

	private DbConfigBean dbConfig;

	protected Shell shell;
	private Combo combo;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;

	private Button button_tableName;
	private Button button_columnName;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public DbConfigDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public DbConfigBean open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return dbConfig;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(687, 611);
		shell.setText(getText());

		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setBounds(41, 20, 106, 20);
		lblNewLabel.setText("TYPE:");

		text = new Text(shell, SWT.BORDER);
		text.setBounds(185, 97, 448, 26);

		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setAlignment(SWT.RIGHT);
		lblNewLabel_1.setBounds(41, 97, 106, 20);
		lblNewLabel_1.setText("DRIVER:");

		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(71, 126, 76, 20);

		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setAlignment(SWT.RIGHT);
		lblNewLabel_3.setBounds(41, 180, 106, 20);
		lblNewLabel_3.setText("URL:");

		Label lblNewLabel_4 = new Label(shell, SWT.NONE);
		lblNewLabel_4.setAlignment(SWT.RIGHT);
		lblNewLabel_4.setBounds(41, 260, 106, 20);
		lblNewLabel_4.setText("USER:");

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setValue();
				File dbConfigFile = new File("database.properties");
				try {
					Files.write(dbConfig.getContent().getBytes("utf-8"), dbConfigFile);
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				shell.dispose();
			}
		});
		btnNewButton.setBounds(210, 443, 98, 30);
		btnNewButton.setText("确定");

		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				dbConfig = null;
				shell.dispose();
			}
		});
		btnNewButton_1.setBounds(401, 443, 98, 30);
		btnNewButton_1.setText("取消");

		Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setAlignment(SWT.RIGHT);
		lblPassword.setText("PASSWORD:");
		lblPassword.setBounds(41, 340, 106, 20);

		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(185, 180, 448, 26);

		text_2 = new Text(shell, SWT.BORDER);
		text_2.setBounds(185, 260, 448, 26);

		text_3 = new Text(shell, SWT.BORDER);
		text_3.setBounds(185, 340, 448, 26);

		String[] db = new String[] { "ORACLE", "MYSQL", "POSTGRESQL" };
		combo = new Combo(shell, SWT.NONE);
		combo.setBounds(185, 20, 162, 28);
		combo.setItems(db);

		button_tableName = new Button(shell, SWT.CHECK);
		button_tableName.setText("表名小写");
		button_tableName.setBounds(210, 396, 121, 20);

		button_columnName = new Button(shell, SWT.CHECK);
		button_columnName.setText("字段名小写");
		button_columnName.setBounds(366, 396, 121, 20);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String value = combo.getText();
				if (value.equals("ORACLE")) {
					dbConfig = DbConfigBean.defaultOracle();
				} else if (value.equals("MYSQL")) {
					dbConfig = DbConfigBean.defaultMysql();
				} else if (value.equals("POSTGRESQL")) {
					dbConfig = DbConfigBean.defaultPostgreSql();
				} else {
					dbConfig = DbConfigBean.defaultMysql();
				}
				setText();
			}
		});
		if (dbConfig != null) {
			if (dbConfig.getDbType() == DbType.ORACLE) {
				combo.select(0);
			} else if (dbConfig.getDbType() == DbType.MYSQL) {
				combo.select(1);
			} else if (dbConfig.getDbType() == DbType.POSTGRESQL) {
				combo.select(2);
			} else {
				MessageDialog.openWarning(shell, "提示", "数据库类型不对!"); 
			}
			setText();
		}
	}

	public void setText() {
		if (dbConfig.getDriver() != null) {
			text.setText(dbConfig.getDriver());
		}
		if (dbConfig.getUrl() != null) {
			text_1.setText(dbConfig.getUrl());
		}
		if (dbConfig.getUser() != null) {
			text_2.setText(dbConfig.getUser());
		}
		if (dbConfig.getPassword() != null) {
			text_3.setText(dbConfig.getPassword());
		}

		button_tableName.setSelection("1".equals(dbConfig.getTableNameLowerCase()));
		button_columnName.setSelection("1".equals(dbConfig.getColumnNameLowerCase()));
	}

	public void setValue() {
		int index = combo.getSelectionIndex();
		if (index == 0) {
			dbConfig.setDbType(DbType.ORACLE);
		} else {
			dbConfig.setDbType(DbType.MYSQL);
		}
		dbConfig.setDriver(text.getText());
		dbConfig.setUrl(text_1.getText());
		dbConfig.setUser(text_2.getText());
		dbConfig.setPassword(text_3.getText());

		dbConfig.setTableNameLowerCase(button_tableName.getSelection() ? "1" : "0");
		dbConfig.setColumnNameLowerCase(button_columnName.getSelection() ? "1" : "0");
	}

	public void setDbConfig(DbConfigBean dbConfig) {
		this.dbConfig = dbConfig;
	}
}
